package dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import dto.RepositoryDTO;

import model.Folder;
import model.Membership;
import model.Repository;

/**
 * @author soreas 
 * 
 * Session Bean implementation class RepositoryDao
 */
@Stateless
public class RepositoryDao {

	@PersistenceContext
	EntityManager em;

	@Resource
	UserTransaction ut;

	/**
	 * Default constructor.
	 */
	public RepositoryDao() {
	}

	/**
	 * @author soreas
	 * @param id
	 *            idul dupa care se cauta in baza de date obiectul Repository
	 * @return obiectul de tip Repository
	 */
	public Repository getRepositoryByID(int pId) {
		Repository r = new Repository();
		try {
			r = em.find(Repository.class, pId);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return r;
	}
	
	/**
	 * @author Zsolt
	 * @param enteredName repository name entered by admin
	 * @return boolean if exist in DB
	 */
	public boolean existsRepositoryName(String enteredName) {
		List<Repository> r = (List<Repository>) em.createQuery("SELECT r FROM Repository r WHERE r.name=?1",Repository.class)
					.setParameter(1, enteredName)
					.getResultList();
		if (r.size()>0) return true;
		else return false;
	}

	/**
	 * @author soreas
	 * @param id idul dupa care se cauta in baza de date obiectul Folder
	 * @return obiectul de tip Folder
	 */
	public Folder getFolderByID(int pId)
	{
		Folder f=new Folder();
		try{
			f=em.find(Folder.class, pId);

		}catch(Exception ex){
			ex.printStackTrace();
		}
		return f;
	}


	/**
	 * @author Zsombor
	 * @param int id - id of folders/repository to search for
	 * @return List of folders associated with given repository
	 */
	public List<Folder> getFoldersById(int id) {
		return em
				.createQuery(
						"Select f from Folder f where f.folder.id=?1 and f.id<>?1",
						Folder.class).setParameter(1, id).getResultList();
	}

	/**
	 * @author Zsombor
	 * @param Folder entity          
	 */

	public void deleteRepository(Folder r) {
		if (r != null) {
			r.setFolder(null);
			em.persist(r);
			em.flush();
			em.remove(r);
		}
	}

	/**
	 * @author Zsombor
	 * @param Folder entity          
	 */
	public void deleteFolder(Folder f){
		em.remove(f);
	}

	/**
	 * @author Zsombor
	 * @param Membership entity          
	 */
	public void persistMembership(Membership m){
		em.persist(m);
	}

	/**
	 * @author Zsombor
	 * @param Repository entity          
	 */
	public void addRepository(Repository r){
		em.persist(r);
		em.flush();
		em.refresh(r);
	}

	public void mergeRepository(Repository r){
		em.merge(r);
	}
	
	/**
	 * @author fodorl
	 * @return integer
	 * @param String repositoryName
	 * returneaza id-ul repository-ului cu numele introdus
	 */
	
	public int getRepositoryByName(String repositoryName){
		int id = em.createQuery(
				"Select r.id from Repository r where r.name=?1", Integer.class).setParameter(1, repositoryName).getSingleResult();
		return id;
	}

	/**
	 * @author Zsolt
	 * @return RepositoryDTO with all repository names
	 */
	public RepositoryDTO getAllRepositoryNames() {
		RepositoryDTO dtoR = new RepositoryDTO();
		List<Repository> rlist = new ArrayList<Repository>();
		rlist = em.createQuery("SELECT r FROM Repository r where r.name<>?1", Repository.class)
				.setParameter(1, "System")
				.getResultList();
		for (Repository r : rlist) {
			dtoR.getAllRepositories().add(r.getName());
		}
		return dtoR;
	}
	
	/**
	 * @author ieneir
	 * @param id -id user 
	 * @return lista repository unde "id" nu este membru
	 */

	public List<Repository> getRepositoriesWhereNotMember(Integer id) {
		return em.createQuery("SELECT r FROM Repository r where r.id not in (select m.folder.id from Membership m where m.user.id=?1) and r.id<>1", Repository.class)
				.setParameter(1,id)
				.getResultList();
	}

	/**
	 * @author ieneir
	 * @return lista repository 
	 */
	public List<Repository> getAllRepositories() {
		return em.createQuery("SELECT r FROM Repository r where r.name<>?1", Repository.class)
				.setParameter(1, "System")
				.getResultList();
	}

	


}
